<template> 
  <div class="app-container">
    <el-card class="filter-container" shadow="never">
      <div style="margin-top: 15px">
        <el-form :inline="true" :model="listQuery" size="small" label-width="140px">
          <el-form-item label="影院所在城市：" prop="locationId">
            <el-cascader
              v-model="location"
              :options="locationOptions"
              :props="{ expandTrigger: 'hover' }"></el-cascader>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              @click="handleSearchList()"
              size="small">
              筛选
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>影院</span>
      <!--      判断是否是管理员，管理员显示添加按钮-->
      <el-button v-if="isAdmin()" size="mini" class="btn-add" @click="handleAdd()" style="margin-left: 20px">添加
      </el-button>
    </el-card>
    <div class="table-container">
      <el-table ref="filmTable"
                :data="list"
                style="width: 100%;"
                :default-sort="{prop:'cinemaName'}"
                v-loading="listLoading" border>
        <el-table-column label="序号" width="100" align="center">
          <template slot-scope="scope">{{
              ((listQuery.pageNum - 1) * listQuery.pageSize) + scope.$index + 1
            }}
          </template>
        </el-table-column>
        <el-table-column label="影院名称" prop="cinemaName" width="200" align="center">
          <template slot-scope="scope">{{ scope.row.cinema.name }}</template>
        </el-table-column>
        <el-table-column sortable prop="locationDetail" label="详细地址" align="center">
          <template slot-scope="scope">{{ scope.row.cinema.locationDetail }}</template>
        </el-table-column>
        <el-table-column label="影厅数量" align="center">
          <template slot-scope="scope">{{ scope.row.rooms.length }}</template>
        </el-table-column>
        <el-table-column label="状态" align="center">
          <template slot-scope="scope">{{ scope.row.cinema.status ? "可用" : "不可用" }}</template>
        </el-table-column>
        <el-table-column label="操作" width="240" align="center">
          <template slot-scope="scope">
            <el-button size="mini"
                       type="text"
                       @click="toFilm(scope.$index, scope.row)">
              上映影片
            </el-button>
            <!--            验证是否是管理员-->
            <el-button size="mini"
                       type="text"
                       v-if="isAdmin()"
                       @click="handleMaintenance(scope.$index, scope.row)">
              维护
            </el-button>
            <el-button size="mini"
                       type="text"
                       v-if="isAdmin()"
                       @click="toStatistics(scope.$index, scope.row)">
              收益
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :current-page.sync="listQuery.pageNum"
        :page-size="listQuery.pageSize"
        :page-sizes="[10,15,20]"
        :total="total">
      </el-pagination>
    </div>
    <el-dialog
      :title="isAdd?'影院':'维护'"
      :visible.sync="maintainDialogVisible"
      width="40%">
      <el-form :model="maintainParams"
               ref="maintainForm"
               label-width="150px" size="small">
        <el-form-item label="影院名">
          <el-input v-model="maintainParams.name" style="width: 250px">
          </el-input>
        </el-form-item>
        <el-form-item label="省">
          <el-input v-model="maintainParams.province" style="width: 250px">
          </el-input>
        </el-form-item>
        <el-form-item label="市">
          <el-input v-model="maintainParams.city" style="width: 250px">
          </el-input>
        </el-form-item>
        <el-form-item label="县/镇">
          <el-input v-model="maintainParams.town" style="width: 250px">
          </el-input>
        </el-form-item>
        <el-form-item label="详细地址">
          <el-input v-model="maintainParams.locationDetail" style="width: 250px">
          </el-input>
        </el-form-item>
        <el-form-item label="是否启用">
          <el-input v-model="maintainParams.status" style="width: 250px">
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="handleDeleteCinema()" style="width: 100px; color: red">删除该影院</el-button>
        </el-form-item>
        <el-form-item>
          <el-button v-if="!isAdd" @click="handleAddRoom(maintainParams.id)" style="width: 100px; color: red">添加影厅</el-button>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="maintainDialogVisible = false" size="small">取 消</el-button>
        <el-button type="primary" @click="handleMaintainConfirm()" size="small">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog
      :title="'新增影厅'"
      :visible.sync="maintainRoomDialogVisible"
      width="40%">
      <el-form :model="maintainRoomParams"
               ref="maintainRoomForm"
               label-width="150px" size="small">
        <el-form-item label="影院 id">
          <el-input v-model="maintainRoomParams.cinemaId" style="width: 250px">
          </el-input>
        </el-form-item>
        <el-form-item label="影厅名">
          <el-input v-model="maintainRoomParams.name" style="width: 250px">
          </el-input>
        </el-form-item>
        <el-form-item label="容量">
          <el-input v-model="maintainRoomParams.capacity" style="width: 250px">
          </el-input>
        </el-form-item>
        <el-form-item label="描述">
          <el-input v-model="maintainRoomParams.description" style="width: 250px">
          </el-input>
        </el-form-item>
        <el-form-item label="是否启用">
          <el-input v-model="maintainRoomParams.status" style="width: 250px">
          </el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="maintainRoomDialogVisible = false" size="small">取 消</el-button>
        <el-button type="primary" @click="handleMaintainRoomConfirm()" size="small">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {
  addCinema,
  addRoom,
  updateCinema,
  deleteCinema,
  filterCinemasBy,
  filterLocationsBy,
  filterAvailableCinemasBy
} from '@/api/cinema'
import {listLevelLocations} from '@/api/location'
import {formatDate} from '@/utils/date';
import {mapGetters} from "vuex";

const defaultListQuery = {
  locationId: null,
  province: "",
  city: "",
  town: "",
  locationDetail: "",
  pageNum: 1,
  pageSize: 10
};
const defaultMaintainParams = {
  id: null,
  name: null,
  province: null,
  city: null,
  town: null,
  status: null
}
const defaultRoomParams = {
  cinemaId: null,
  name: "",
  capacity: 180,
  description: "",
  status: true
}

export default {
  name: 'index',
  data() {
    return {
      maintainParams: Object.assign({}, defaultMaintainParams),
      maintainRoomParams: Object.assign({}, defaultRoomParams),
      listQuery: Object.assign({}, defaultListQuery),
      locationOptions: [],
      location: [],
      list: [],
      total: null,
      listLoading: false,
      arrangeDialogVisible: false,
      maintainDialogVisible: false,
      maintainRoomDialogVisible: false,
      isAdd: true,
      isEdit: false
    }
  },
  created() {
    this.getList()
    this.getLocationList()
  },
  filters: {
    formatDateTime(time) {
      if (time == null || time === '') {
        return '';
      }
      let date = new Date(time);
      return formatDate(date, 'yyyy-MM-dd hh:mm:ss')
    }
  },
  computed: {
    ...mapGetters([
      'username'
    ])
  },
  methods: {
    isAdmin() {
      return this.username === 'admin'
    },
    handleAdd() {
      this.maintainParams = defaultMaintainParams;
      this.maintainDialogVisible = true;
      this.isAdd = true;
    },
    handleAddRoom(cinemaId) {
      this.maintainRoomParams = defaultRoomParams;
      this.maintainRoomParams.cinemaId = cinemaId
      this.maintainRoomDialogVisible = true;
    },
    handleMaintenance(index, row) {
      this.maintainParams.id = row.cinema.id
      this.maintainParams.name = row.cinema.name
      this.maintainParams.locationId = row.location.id
      this.maintainParams.province = row.location.province
      this.maintainParams.city = row.location.city
      this.maintainParams.town = row.location.town
      this.maintainParams.status = row.cinema.status
      this.maintainDialogVisible = true
      this.isAdd = false
    },
    toFilm(index, row) {
      this.$router.push({
        name: 'film', params: {
          cinemaId: row.cinema.id
        }
      });
    },
    toStatistics(index, row) {
      this.$router.push({
        name: 'statistics', params: {
          cinemaId: row.cinema.id
        }
      });
    },
    getList() {
      this.listLoading = true;
      filterAvailableCinemasBy(this.listQuery).then(res => {
        this.listLoading = false;
        this.list = res.data.list;
        this.total = res.data.total;
      });
    },
    getLocationList() {
      listLevelLocations(this.listQuery).then(res => {
        this.locationOptions = res.data;
        console.log(res.data)
      })
    },
    handleResetSearch() {
      this.listQuery = Object.assign({}, defaultListQuery);
    },
    handleSearchList() {
      this.listQuery.pageNum = 1;
      if (this.location[2]) {
        this.listQuery.locationId = this.location[2];
      } else if (this.location[1]) {
        this.listQuery.locationId = this.location[1];
      } else if (this.location[0]) {
        this.listQuery.locationId = this.location[0];
      } else {
        this.$message.error("请先选择城市");
        return;
      }
      this.getList();
    },
    handleSizeChange(val) {
      this.listQuery.pageNum = 1;
      this.listQuery.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.pageNum = val;
      this.getList();
    },
    handleMaintainConfirm() {
      this.$confirm('确认?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        if (this.isAdd) {
          addCinema(this.maintainParams).then(response => {
            this.$message({
              message: response.data,
              type: 'success'
            });
            this.maintainDialogVisible = false;
          }).catch(() => {
            this.maintainDialogVisible = false;
          });
        } else {
          updateCinema(this.maintainParams).then(response => {
            this.$message({
              message: response.data,
              type: 'success'
            });
            this.maintainDialogVisible = false;
          }).catch(() => {
            this.maintainDialogVisible = false;
          });
        }
      })
    },
    handleDeleteCinema() {
      this.$confirm('确认删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteCinema(this.maintainParams.id).then(response => {
          this.$message({
            message: "成功禁用",
            type: 'success'
          });
          this.maintainDialogVisible = false;
        }).catch(() => {
          this.maintainDialogVisible = false;
          this.$message({
            message: "禁用失败",
            type: 'error'
          });
        });
      })
    },
    handleMaintainRoomConfirm() {
      this.$confirm('确认添加?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        addRoom(this.maintainRoomParams).then(response => {
          this.$message({
            message: "添加成功",
            type: 'success'
          });
          this.maintainRoomDialogVisible = false;
          this.maintainDialogVisible = false
        }).catch(() => {
          this.maintainRoomDialogVisible = false;
          this.$message({
            message: "添加失败",
            type: 'error'
          });
        });
      })
    }
  }
}
</script>
<style></style>


